<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>角色管理</title>
    <link rel="icon" href="../assets/images/favicon.ico"/>
    <link rel="stylesheet" href="../assets/libs/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../assets/common.css" media="all">
</head>

<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body layui-form">
            <button id="authBtnSync" class="layui-btn icon-btn"><i class="layui-icon">&#xe669;</i>同步权限</button>

            <!-- 数据表格 -->
            <table class="layui-table" id="authTable" lay-filter="authTable"></table>
        </div>
    </div>
</div>

<!-- js部分 -->
<script type="text/javascript" src="../assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="../assets/common.js"></script>
<script>
    layui.use(['jquery', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        table.render({
            elem: '#authTable',
            url: base_server + 'authorities',
            where: {
                access_token: admin.getToken()
            },
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'authorityName', sort: true, title: '接口名称'},
                {field: 'authority', sort: true, title: '权限标识'},
                {
                    field: 'createTime', sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '同步时间'
                }
            ]],
            skin: 'line',
            size: 'lg'
        });

        // 同步按钮点击事件
        $('#authBtnSync').click(function () {
            layer.confirm('确定进行同步吗？<br><span style="color: red;">（自动扫描所有接口生成权限标识）</span>', function () {
                layer.load(2);
                $.get('/v2/api-docs', function (data) {
                    admin.req('authorities/sync', {
                        json: JSON.stringify(data)
                    }, function (res) {
                        layer.closeAll('loading');
                        if (200 == res.code) {
                            layer.msg(res.msg, {icon: 1});
                            table.reload('authTable');
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'POST');
                });
            });
        });

    });
</script>

</body>
</html>